<?php
!defined("IN_YCTOOL") && exit();
/**
 * phpmailer's parameters
 */
require_once(DIR_COMM . 'phpmailer/class.phpmailer.php');

function SendMail($to, $subject = '', $message = '', $from = SENDMAIL_FROM, 
    $fromname = SENDMAIL_FROMNAME, $ishtml = true, 
    $emailcharset = Email_CHARSET, $syscharset = CHARSET, $pauth = SENDMAIL_SMTPAUTH,
    $server = SENDMAIL_HOST, $port = SENDMAIL_PORT, 
    $user = SENDMAIL_USERNAME, $pass = SENDMAIL_PASSWORD)
{
    if($emailcharset != $syscharset)
    {
	$subject = iconv($syscharset, $emailcharset, $subject);
	$message = iconv($syscharset, $emailcharset, $message);
	$fromname = iconv($syscharset, $emailcharset, $fromname);
    }

    if(SENDMAIL_METHOD == 'public')
    {
	    require_once(DIR_COMM . 'phpmailer/public.smtp.php');
	    $smtpserver = $server;//SMTP服务器
	    $smtpserverport = $port;//SMTP服务器端口
	    $smtpusermail = $from ;//SMTP服务器的用户邮箱
	    $smtpemailto = $to;//发送给谁
	    $smtpuser = $user;//SMTP服务器的用户帐号
	    $smtppass = $pass;//SMTP服务器的用户密码
	    $mailsubject = $subject;//邮件主题
            $mailbody = $message;//邮件内容
            if($ishtml)
            {
                $mailtype = "HTML";//邮件格式（HTML/TXT）,TXT为文本邮件
            }
            else
            {
                $mailtype = "TXT";//邮件格式（HTML/TXT）,TXT为文本邮件     
            }
	    
	    $smtp = new smtp($smtpserver, $smtpserverport ,true, $smtpuser, $smtppass);//这里面的一个true是表示使用身份验证,否则不使用身份验证.
	    if(DB_DEBUG)
	    {
		$smtp->debug = TRUE;//是否显示发送的调试信息
	    }
	    return $smtp->sendmail($smtpemailto, $smtpusermail, $mailsubject, $mailbody, $mailtype);
    }
    else
    {
	$mail = new PHPMailer();
	switch (SENDMAIL_METHOD)
	{
	case "smtp":
	    $mail->IsSMTP();
		break;
	case "mail":
		$mail->IsMail();
		break;
	case "sendmail":
		$mail->IsSendmail();
		break;
	case "qmail":
		$mail->IsQmail();
		break;
	default:
		$mail->IsMail();
	}

	$mail->SMTPDebug = false;
	$mail->Host = SENDMAIL_HOST;
	$mail->SMTPAuth = SENDMAIL_SMTPAUTH;
	$mail->Username = SENDMAIL_USERNAME;
	$mail->Password = SENDMAIL_PASSWORD;

	$mail->From = SENDMAIL_FROM;
	$mail->FromName = SENDMAIL_FROMNAME;
	$mail->CharSet = $emailcharset;
	$toArray = explode(",", $to);

	foreach ($toArray as $item)
	{
		$mail->AddAddress($item);
	}

	if ($from)
	{
		$mail->AddReplyTo($from, $fromname);
		$mail->From = $from;
		$mail->FromName = $fromname;
	}
	else
	{
		$mail->AddReplyTo(SENDMAIL_FROM, SENDMAIL_FROMNAME);
	}
	$mail->IsHtml($ishtml);
	$mail->Subject = $subject;
	$mail->Body = $message;
	if(DB_DEBUG)
	{
	    if(!$mail->Send()) 
	    { 
		echo "邮件发送有误 <p>"; 
		echo "邮件错误信息: " . $mail->ErrorInfo; 
	    } 
	    else
	    {
		echo "邮件发送成功!<br />"; 
	    }  
	}
	return $mail->Send(); 
    }
}
?>
